home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 September / CHIP Eylül 1998.iso / Slackwar / docs / mini / DHCP < prev    next >
Text File  |  1996-12-15  |  9KB  |  236 lines

  1.  
  2. How to get Linux to use DHCP information from Windows 95
  3.  
  4. Last revised: December 6, 1996
  5.  
  6. Dan Halbert
  7. halbert@world.std.com
  8. halbert@bbn.com
  9.  
  10.  
  11. Introduction
  12. ============
  13.  
  14. My Linux PC needs to get its IP address and other network information
  15. from a DHCP server. However, I have been unable to find a DHCP client
  16. that works well with the DHCP server I use.
  17.  
  18. My PC normally boots up with Windows 95, and Linux is used less often.
  19. The Windows 95 DHCP client works fine for me. I could manually copy
  20. my IP address, nameservers, etc., over to the Linux side, but I wanted
  21. to automate that process.
  22.  
  23. Win95 supplies a program (\Windows\Winipcfg.exe) that displays the
  24. DHCP info in a nice graphical way. By examining the strings in
  25. winipcfg.exe, I discovered that it has undocumented options that will
  26. also write the information to a file ("Winipcfg.out", by default).
  27. This allowed me to work out the following trick:
  28.  
  29. 1. Boot Win95. Its DHCP client will get the necessary information from
  30.    the DHCP server.
  31. 2. Run Winipcfg.exe, and write the results to a file.
  32. 3. Boot Linux.
  33. 4. Mount the Win95 file system.
  34. 5. Parse the file produced by Winipcfg.
  35. 6. Supply Linux with the appropriate networking information.
  36.  
  37.  
  38. Method
  39. ======
  40.  
  41. Now the details of how I do it, for my particular setup:
  42.  
  43. 1. In my Win95 StartUp folder, I run this command. When Win95 boots,
  44. Winipcfg records the DHCP information Win95 has retrieved from the
  45. DHCP server:
  46.  
  47.         winipcfg -all -batch c:\winipcfg.out
  48.  
  49. The options must be in given order. Omitting "-all" omits some of the
  50. information. Omitting the filename writes "winipcfg.out" to the current
  51. directory. A sample winipcfg.out file is attached below.
  52.  
  53.  
  54. 2. Immediately (or sometime later) after booting Win95, I boot Linux.
  55. I use loadlin, run from an MS-DOS-mode batch file. You might use that
  56. or something else.
  57.  
  58. (It is not possible to combine the Winipcfg and the loadlin into a
  59. single batch file, because apparently the "winipcfg.out" file does not
  60. get flushed from the disk cache before loadlin runs. I haven't found
  61. out any way of flushing the cache from the command line.)
  62.  
  63.  
  64. 3. When Linux boots, the initial boot script runs "winipcfg.pl", a
  65. perl program supplied at the end of this mini-HOWTO. This program must
  66. be run after the Win95 file system has been mounted, but before
  67. running the "network" script. I use the Debian Linux distribution:
  68. attached below is an excerpt from Debian's /etc/init.d/boot, with my
  69. additions noted. Your distribution may have different boot scripts, so
  70. you may need to adapt this. The "winipcfg.pl" program writes new
  71. versions of /etc/hostname and /etc/resolv.conf, and the
  72. /etc/init.d/network script. The "network" script does an "ifconfig"
  73. with appropriate arguments.
  74.  
  75. That's all there is to it. Note of course that you should not leave
  76. your Linux system up for longer than the duration of the DHCP lease. I
  77. only boot for a few hours at a time, so that's not a problem for me.
  78.  
  79. You may need to alter the "winipcfg.pl" script if you have more than
  80. one network adapter or pseudo-adapter. For instance, AOL software sets
  81. up a fake network adapter, and you need to ignore its information.
  82.  
  83. Attachments follow, one per page.
  84.  
  85. ==============================================================================
  86. Sample winipcfg.out produced by Win95 "winipcfg -all -batch"
  87. (addresses changed to protect the innocent)
  88. ==============================================================================
  89. Windows 95 IP Configuration
  90.  
  91.         Host Name . . . . . . . . . : foo.bar.com
  92.         DNS Servers . . . . . . . . : 11.222.33.8
  93.                                       11.222.33.9
  94.         Node Type . . . . . . . . . : Broadcast
  95.         NetBIOS Scope ID. . . . . . : 
  96.         IP Routing Enabled. . . . . : No
  97.         WINS Proxy Enabled. . . . . : No
  98.         NetBIOS Resolution Uses DNS : Yes
  99.  
  100. Ethernet adapter :
  101.  
  102.         Description . . . . . . . . : ELNK3 Ethernet Adapter
  103.         Physical Address. . . . . . : 00-AA-BB-99-88-77
  104.         DHCP Enabled. . . . . . . . : Yes
  105.         IP Address. . . . . . . . . : 11.222.33.222
  106.         Subnet Mask . . . . . . . . : 255.255.255.0
  107.         Default Gateway . . . . . . : 11.222.33.1
  108.         DHCP Server . . . . . . . . : 11.222.33.10
  109.         Primary WINS Server . . . . : 
  110.         Secondary WINS Server . . . : 
  111.         Lease Obtained. . . . . . . : Wed Oct 16 96 10:17:26 PM
  112.         Lease Expires . . . . . . . : Wed Oct 30 96 10:17:26 PM
  113. ==============================================================================
  114.  
  115. ==============================================================================
  116. Excerpt from Debian's /etc/init.d/boot, with my addition.
  117. ==============================================================================
  118. ...
  119. # Mount local file systems in /etc/fstab.
  120. echo "Mounting local file systems..."
  121. mount -avt nonfs
  122.  
  123. # Execute swapon command again, in case we want to swap to
  124. # a file on a now mounted filesystem.
  125. swapon -a 2>/dev/null
  126.  
  127. #### DHCP info addition ####################################################
  128. # Update network information files with info from Win95 DHCP client.
  129. if [ -f /c/winipcfg.out -a -x /usr/local/sbin/winipcfg.pl ]
  130. then
  131.   echo "Setting network information from Windows 95 DHCP info."
  132.   # This writes new versions of hostname, resolv.conf, and init.d/network.
  133.   # /c/ is where I mount the Win95 file system.
  134.   /usr/local/sbin/winipcfg.pl /c/winipcfg.out
  135.   if [ $? != 0 ]
  136.   then
  137.     echo "Error getting network information. Using existing network info."
  138.   fi
  139. else
  140.   echo "** Windows 95 DHCP info not available. Using existing network info."
  141. fi
  142. #### end of DHCP info addition #############################################
  143.  
  144. # Setup the network interfaces. Note that /var/run and /var/lock
  145. # are cleaned up after this, so don't put anything in the "network"
  146. # script that leave a pidfile or a lockfile.
  147. if [ -x /etc/init.d/network ]
  148. then
  149.   /etc/init.d/network
  150. fi
  151.  
  152. # Set hostname.
  153. hostname --file /etc/hostname
  154.  
  155. # Now that TCP/IP is configured, mount the NFS file systems in /etc/fstab.
  156. echo "Mounting remote file systems..."
  157. mount -a -t nfs
  158.  
  159. ...
  160. ==============================================================================
  161.  
  162. ==============================================================================
  163. /usr/local/sbin/winipcfg.pl:
  164. This is the perl program invoked from the boot script above.
  165. ==============================================================================
  166. #!/usr/bin/perl
  167.  
  168. while (<>) {
  169.     # Remove any trailing CR and LF.
  170.     s/\r//g;
  171.     s/\n//g;
  172.  
  173.     if (/^\tHost Name.*: (.*)$/i) {
  174.         ($hostname, $domain) = split(/\./, $1, 2);
  175.     } elsif (/^\tDNS Servers.*: *(.*)$/i) {
  176.         push @nameservers, $1;
  177.     } elsif (/^\t +([\.0-9]+)$/) {
  178.         push @nameservers, $1;
  179.     } elsif (/^\tIP Address.*: (.*)$/i) {
  180.         $ip_address = $1;
  181.     } elsif (/^\tSubnet Mask.*: (.*)$/i) {
  182.         $netmask = $1;
  183.     } elsif (/^\tDefault Gateway.*: (.*)$i/) {
  184.         $gateway = $1;
  185.     }
  186. }
  187.  
  188. $hostname || die "Hostname not given";
  189. $domain || die "Domain not given";
  190. $ip_address || die "IP address not given";
  191. $netmask || die "Netmask not given";
  192. $gateway || die "Gateway not given";
  193. $#nameservers != -1 || die "No nameservers given";
  194.  
  195. open(HOSTNAME, ">/etc/hostname") ||
  196.     die "Could not open /etc/hostname for writing\n";
  197. open(RESOLV, ">/etc/resolv.conf") ||
  198.     die "Could not open /etc/resolv.conf for writing\n";
  199. open(NETWORK, ">/etc/init.d/network") ||
  200.     die "Could not open /etc/init.d/network for writing\n";
  201. chmod 0755, "/etc/init.d/network";
  202.  
  203. print HOSTNAME "$hostname\n";
  204. print RESOLV "domain $domain\n";
  205. print RESOLV "search $domain\n";
  206. foreach (@nameservers) {
  207.     print RESOLV "nameserver $_\n";
  208. }
  209.  
  210. @ip_address = split(/\./, $ip_address, 4);
  211. @netmask = split(/\./, $netmask, 4);
  212. $network = join(".",
  213.                 $ip_address[0]+0 & $netmask[0]+0,
  214.                 $ip_address[1]+0 & $netmask[1]+0,
  215.                 $ip_address[2]+0 & $netmask[2]+0,
  216.                 $ip_address[3]+0 & $netmask[3]+0);
  217. $broadcast = join(".",
  218.                   $ip_address[0]+0 | ~($netmask[0]+0) & 255,
  219.                   $ip_address[1]+0 | ~($netmask[1]+0) & 255,
  220.                   $ip_address[2]+0 | ~($netmask[2]+0) & 255,
  221.                   $ip_address[3]+0 | ~($netmask[3]+0) & 255);
  222. print NETWORK
  223. "ifconfig eth0 $ip_address netmask $netmask broadcast $broadcast
  224. route add -net $network netmask $netmask
  225. route add default gw $gateway metric 1
  226. ";
  227.  
  228. print
  229. "DHCP info: $hostname.$domain ($ip_address)
  230.   netmask: $netmask broadcast: $broadcast gateway: $gateway
  231.   nameservers: @nameservers
  232. ";
  233. exit(0);
  234. ==============================================================================
  235.  
  236.